import pymysql
from datetime import datetime

# 配置连接信息
connection = pymysql.connect(
    host='172.19.251.204',  # WSL 2 的 IP 地址
    user='meiduo_user',
    password='123456',      # meiduo_user 的密码
    port=3306,
    database='meiduo_mall'  # 指定要使用的数据库
)

try:
    with connection.cursor() as cursor:
        # Insert data into the django_migrations table
        insert_query = "INSERT INTO django_migrations (id, app, name, applied) VALUES (%s, %s, %s, %s)"
        data = (27, 'orders', '0001_initial', datetime(2025, 6, 26, 2, 32, 44, 495111))
        cursor.execute(insert_query, data)
        # Commit the transaction
        connection.commit()
        print("Data inserted successfully")

        # Query all data from the django_migrations table
        cursor.execute("SELECT * FROM django_migrations;")

        # Get all column names
        column_names = [desc[0] for desc in cursor.description]
        print("Columns:", column_names)

        # Get all data
        rows = cursor.fetchall()

        # Print each row of data
        for row in rows:
            print(row)

except pymysql.Error as e:
    print(f"Database error: {e}")
    connection.rollback()
finally:
    connection.close()